
import database.Mysql;
import entity.Backup;
import java.io.File;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

/**
 *
 * @author Jason 14-Jul-2011
 */
public class DatabaseUitl {

    private static String FROM = "software@lotary.com.cn";
    private static String TO = "software0@lotary.com.cn";

    public static void backupDatabase() {
        StringBuilder result = new StringBuilder(ServerIp + "<br/>Backup at : " + new Date() + "<br/><br/><hr/>");
        StringBuilder subject = new StringBuilder("Backup Database --- ");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        for (Backup b : Mysql.getBackups()) {
            String command = "mysqldump -h "
                    + b.getHost()
                    + " -u " + b.getUserName()
                    + " -p"
                    + b.getPassword()
                    + " "
                    + b.getDatabaseName()
                    + " |gzip > "
                    + b.getPath()
                    + b.getDatabaseName()
                    + dateFormat.format(new Date()) + ".sql.gz";
            String command_output = "mysqldump -h "
                    + b.getHost()
                    + " -u " + b.getUserName()
                    + " -p password  "
                    + b.getDatabaseName()
                    + " |gzip > "
                    + b.getPath()
                    + b.getDatabaseName()
                    + dateFormat.format(new Date()) + ".sql.gz";
            try {
                if (!checkPath(b.getPath())) {
                    result.append("<span style='color:red;'>path: [").append(b.getPath()).append("] can not found or can not write !").append("<br/></span>");
                }
                Process process = Runtime.getRuntime().exec(new String[]{"bash", "-c", command});
                if (process.waitFor() != 0) {
                    result.append("<span style='color:red'>Falid ** </span>");
                }
                result.append("command : ").append(command_output).append("<br/>");
                process.destroy();
            } catch (Exception ex) {
                subject.append("<a style='color:red;'>");
                subject.append(ex.getMessage());
                subject.append("</a>");
                subject.append("<hr/>");
            }
        }
        String body = result.toString();
        String title = subject.toString();
        title = title + (body.contains("Falid") ? "Falid important" : "Normal");
        Mailer.sendMail(FROM, TO, title, result.toString());
    }

    private static boolean checkPath(String path) {
        File file = new File(path);
        return file.exists() && file.isDirectory() && file.canWrite();
    }
    private static final String ServerIp = getServerIp();

    private static String getServerIp() {
        try {
            Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;

            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();

                if (netInterface.getName().equals("lo")) {
                    continue;
                }
                Enumeration addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    ip = (InetAddress) addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address) {
                        return ip.getHostAddress();
                    }
                }
            }
        } catch (SocketException ex) {
            //TODO: log
        }
        return null;
    }
}
